﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFSystem.JXC.BLL;
using CFSystem.JXC.Model;

namespace CFSystem.JXC.BasisInfo
{
    public partial class FrmProductCatergory : Form
    {
        FrmEditDialog_PrdCgy frmEdlg = null;
        private bool isCancel = false;   //indicates edit dialog be cancelled.
        public FrmProductCatergory()
        {
            InitializeComponent();
            // initTreeViewDisplay();
            initProdCatergoryDisplay();
            this.Text = "商品类别设置";
            this.LoadAllProdCategory("编号", "类别名称", "隶属于");
            ucBaseSetting.OnAddEvent += new UCBaseSetting.UCBaseSettingAddhandler(AddBaseSetting_Prod);
            ucBaseSetting.OnDelEvent += new UCBaseSetting.UCBaseSettingDelhandler(DelBaseSetting_Prod);
            ucBaseSetting.OnUpdEvent += new UCBaseSetting.UCBaseSettingUpdhandler(UpdateBaseSetting_Prod);
        }
        /// <summary>
        /// 
        /// </summary>
        private void AddBaseSetting_Prod()
        {
            List<string> lstCatergoryInfo = GetInput();
            ProdCatergory PC = new ProdCatergory();

            if (!this.isCancel)
            {
                if (lstCatergoryInfo[0] != string.Empty && lstCatergoryInfo[1] != string.Empty)
                {
                    if (!PC.IsExists(lstCatergoryInfo[0], lstCatergoryInfo[1]))
                    {
                        PC.Insert(lstCatergoryInfo[1], lstCatergoryInfo[0]);
                        //reload Brands to display
                        this.LoadAllProdCategory("编号", "类别名称", "隶属于");
                        frmEdlg.LoadBlongsToList();  //refreh BlongsToList.
                        this.TopLevel = true;
                    }
                    else
                    {
                        MessageBox.Show("该类别已经存在！");
                    }
                }
                else
                {
                    MessageBox.Show("内容不能为空");
                }
            }
            this.isCancel = false;
        }
        /// <summary>
        /// 
        /// </summary>
        private void DelBaseSetting_Prod()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            ProdCatergory PC = new ProdCatergory();
            if (delRowID > 0)
            {
                PC.Delete(delRowID);//, lstCatergoryInfo[0], lstCatergoryInfo[1]);
                //reload Brands to display
                this.LoadAllProdCategory("编号", "类别名称", "隶属于");
                if (frmEdlg != null)
                {
                    frmEdlg.LoadBlongsToList();  //refreh BlongsToList.
                }
                this.TopLevel = true;
            }
            else
            {
                MessageBox.Show("根不能删除！");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void UpdateBaseSetting_Prod()
        {
            int delRowID = ucBaseSetting.CurrentSelectedID;
            List<string> lstCatergoryInfo = GetInput();
            ProdCatergory PC = new ProdCatergory();

            if (!this.isCancel)
            {
                if (lstCatergoryInfo[0] != string.Empty && lstCatergoryInfo[1] != string.Empty)
                {
                    if (!PC.IsExists(lstCatergoryInfo[0], lstCatergoryInfo[1]))
                    {
                        PC.Update(delRowID, lstCatergoryInfo[0], lstCatergoryInfo[1]);
                        //reload Brands to display
                        this.LoadAllProdCategory("编号", "类别名称", "隶属于");
                        frmEdlg.LoadBlongsToList();  //refreh BlongsToList.
                        this.TopLevel = true;
                    }
                    else
                    {
                        MessageBox.Show("该类别已经存在！");
                    }
                }
                else
                {
                    MessageBox.Show("内容不能为空！");
                }
            }
            this.isCancel = false;
        }

        /// <summary>
        /// get the input value for update/add.
        /// </summary>
        /// <returns></returns>
        private List<string> GetInput()
        {
            if (frmEdlg == null)
            {
                frmEdlg = new FrmEditDialog_PrdCgy();
            }
            frmEdlg.TopLevel = true;
            frmEdlg.ShowDialog();
            string name = frmEdlg.Catergory;
            string parentName = frmEdlg.BelongsTo;
            frmEdlg.Catergory = string.Empty;   //clear
            frmEdlg.BelongsTo = string.Empty;   //clear
            this.isCancel = frmEdlg.IsCancelled;
            frmEdlg.IsCancelled = false;
            frmEdlg.Hide();

            List<string> lst = new List<string>();
            lst.Add(name);
            lst.Add(parentName);
            return lst;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="firstColName"></param>
        /// <param name="SecondColName"></param>
        /// <param name="thirdColName"></param>
        private void LoadAllProdCategory(string firstColName, string SecondColName, string thirdColName)
        {
            #region construct columns
            //initial grid
            DataTable dt = new DataTable();
            //Create a new column for datatable.
            DataColumn dc = new DataColumn(firstColName, System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            //Create a new column for datatable.
            dc = new DataColumn(SecondColName, System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            //Create a new column for datatable.
            dc = new DataColumn(thirdColName, System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            #endregion

            DataRow dr;

            ProdCatergory PC = new ProdCatergory(); //BLL calling.
            IList<ProdCatergoryInfo> IPCInfo = PC.GetProdCatergorys();
            foreach (ProdCatergoryInfo PCI in IPCInfo)
            {
                dr = dt.NewRow();
                dr[firstColName] = PCI.ProdCatergoryID.ToString();
                dr[SecondColName] = PCI.ProdCatergoryName.ToString();
                dr[thirdColName] = PCI.ProdCatergoryParentName.ToString();
                dt.Rows.Add(dr);
            }

            ucBaseSetting.DataFilled = dt;
            IList<int> colwith = new List<int>();
            colwith.Add(40);
            colwith.Add(83);
            colwith.Add(83);
            ucBaseSetting.PopulateGridData(colwith);
            TreeViewDisplay(dt);
        }

        private void initProdCatergoryDisplay()
        {
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtCatergory"></param>
        private void TreeViewDisplay(DataTable dtCatergory)
        {
            #region obsolete
            //throw new NotImplementedException();
            //DataTable catTable;

            //catTable = MakeNamesTable();
            //DataRow dr;

            //dr = catTable.NewRow();

            //dr["ID"] = "1";

            //dr["typeID"] = "裤子";

            //dr["parentID"] = "服装";

            //catTable.Rows.Add(dr);

            //dr = catTable.NewRow();

            //dr["ID"] = "2";

            //dr["typeID"] = "上衣";

            //dr["parentID"] = "服装";

            //catTable.Rows.Add(dr);

            //dr = catTable.NewRow();

            //dr["ID"] = "3";

            //dr["typeID"] = "帽子";

            //dr["parentID"] = "服装";

            //catTable.Rows.Add(dr);

            //dr = catTable.NewRow();

            //dr["ID"] = "4";

            //dr["typeID"] = "女帽";

            //dr["parentID"] = "帽子";

            //catTable.Rows.Add(dr);

            //dr = catTable.NewRow();

            //dr["ID"] = "5";

            //dr["typeID"] = "服装";

            //dr["parentID"] = "所有类别";

            //catTable.Rows.Add(dr);
            #endregion

            try
            {
                TreeNode rootnode = new TreeNode();
                rootnode.Text = "所有类别";
                TVDisplay.Nodes.Clear();
                TVDisplay.Nodes.Add(rootnode);
                TreeViewDisplay(dtCatergory, "所有类别", rootnode);
                TVDisplay.ExpandAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="catTable"></param>
        /// <param name="ParentID"></param>
        /// <param name="tn"></param>
        private void TreeViewDisplay(DataTable catTable, string ParentID, TreeNode tn)
        {
            DataRow[] dt = catTable.Select("隶属于='" + ParentID + "'"); //find childs by parentID.

            foreach (DataRow dr in dt)
            {
                //if (dr["parentID"] != null)
                if (dr["类别名称"] != null) //has child ,added, -digui condition.
                {
                    TreeNode child = new TreeNode();
                    child.Text = dr["类别名称"].ToString();
                    tn.Nodes.Add(child);
                    TreeViewDisplay(catTable, dr["类别名称"].ToString(), child); //typeID is the parent of next node digui.
                }
            }
        }

        private DataTable MakeNamesTable()
        {
            // Create a new DataTable titled 'Names.'

            DataTable namesTable = new DataTable("ProdCatergory");

            // Add three column objects to the table.
            DataColumn idColumn = new DataColumn();
            idColumn.DataType = System.Type.GetType("System.String");
            idColumn.ColumnName = "ID";
            idColumn.Caption = "编号";
            //idColumn.AutoIncrement = true;
            namesTable.Columns.Add(idColumn);

            DataColumn fNameColumn = new DataColumn();
            fNameColumn.DataType = System.Type.GetType("System.String");
            fNameColumn.ColumnName = "typeID";
            fNameColumn.Caption = "类别";
            //fNameColumn.DefaultValue = "Fname";
            namesTable.Columns.Add(fNameColumn);

            DataColumn lNameColumn = new DataColumn();
            lNameColumn.DataType = System.Type.GetType("System.String");
            lNameColumn.ColumnName = "parentID";
            lNameColumn.Caption = "隶属于";
            namesTable.Columns.Add(lNameColumn);
            // Create an array for DataColumn objects.

            DataColumn[] keys = new DataColumn[1];
            keys[0] = idColumn;
            namesTable.PrimaryKey = keys;
            // Return the new DataTable.

            return namesTable;
        }


    }
}
